#include "modulebutton.h"
#include "ui_modulebutton.h"
#include <QBrush>

ModuleBtn::ModuleBtn(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModuleBtn)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    // 设置背景图
    ui->background->resize(this->size());
    ui->background->setPixmap(QPixmap(":/buttonbg.png"));
    ui->background->setScaledContents(true);

    QIcon icon;
    ui->close->setFlat(true);
    icon.addFile(tr(":/close.png"));
    ui->close->setIconSize(QSize(30, 30));
    ui->close->setIcon(icon);
    QString pressStyle =
            "QPushButton:pressed{ border: 60px; }";
    ui->button->setStyleSheet(pressStyle);

    QString nameStyle =
            "font-family:方正黑体; font-size:28px";
    ui->name->setStyleSheet(nameStyle);

    m_pathFile =  QCoreApplication::applicationDirPath() + "/Config.ini";
}

ModuleBtn::~ModuleBtn()
{
    delete ui;
}

void ModuleBtn::SetModuleIcon(QIcon* icon) {
    ui->button->setFlat(true);
    ui->button->setIconSize(QSize(220, 220));
    ui->button->setIcon(*icon);
}

void ModuleBtn::SetModuleText(const QString& text) {
    if(text == "添加模块") {
        ui->close->hide();
    }
    ui->name->setText(text);
}

void ModuleBtn::SetModuleIndex(const int& index, bool add) {
    m_index = index;
    m_add = add;
}

void ModuleBtn::on_close_clicked()
{
    QVector<QString> vpath;
    QSettings* configure = new QSettings(m_pathFile, QSettings::IniFormat);
    configure->setIniCodec("UTF8");
    QString str = QString("module/path");

    QString pathfile;
    QStringList pathlist = configure->value(str).toString().split(":");
    for(int i = 0; i < pathlist.size(); ++i) {
        if(pathlist.at(i) != "") {
            vpath.push_back(pathlist.at(i));
        }
    }

    vpath.erase(vpath.begin() + m_index);

    for (int i = 0; i < vpath.size(); ++i) {
        if(i != 0){
            pathfile += ":";
        }
        pathfile += vpath[i];
    }

    configure->setValue("module/path", pathfile);

    delete configure;
    emit sendCloseIndex(m_index);

    //this->close();
}

void ModuleBtn::on_button_clicked()
{
    if(m_add == true) {
        QVector<QString> vpath;
        QSettings* configure = new QSettings(m_pathFile, QSettings::IniFormat);
        configure->setIniCodec("UTF8");
        QString str = QString("module/path");

        QString pathfile = configure->value(str).toString();
        QStringList pathlist = pathfile.split(":");
        for(int i = 0; i < pathlist.size(); ++i) {
            if(!vpath.contains(pathlist.at(i)) && pathlist.at(i) != "") {
                vpath.push_back(pathlist.at(i));
            }
        }

        //选择共享库并添加
        QStringList fileNames = QFileDialog::getOpenFileNames(nullptr, "文件选择", "", tr("动态库(*.dll *.so *.dylib);;"));

        for (int i = 0; i < fileNames.size(); ++i) {
            if(!vpath.contains(fileNames[i])) {
                if(pathfile != "") {
                    pathfile += ":";
                }
                pathfile += fileNames[i];
            }
            else {
                cout << "共享库 " << fileNames[i] << " 无效或者被重复添加！";
            }
        }

        configure->setValue("module/path", pathfile);
        delete configure;

        emit sendRefresh();
    }
    else {
        emit sendModuleIndex(m_index);
    }
}
